﻿using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input.Touch;

namespace OfficialGameProject {
	internal class EffectHandler {
		private SpriteBatch spriteBatch;
		private ContentManager contentManager;
		private SpriteFont debugFont;
		private TouchCollection touchCollection;
		private List<SwordSlash> listSwordSlashes;
		private Texture2D swordSlash;
		private Vector2 lastPos;
		private Vector2 currentPos;
		private Vector2 tempPos;
		private double distance;
		private int additionElements;

		/// <summary>
		/// The Constructor of this class
		/// </summary>
		/// <param name="contentManager"></param>
		/// <param name="spriteBatch"></param>
		public EffectHandler(ContentManager contentManager, SpriteBatch spriteBatch) {
			this.contentManager = contentManager;
			this.spriteBatch = spriteBatch;
			listSwordSlashes = new List<SwordSlash>();
			tempPos = new Vector2(-30, -30);
		}

		public void LoadContent() {
			swordSlash = contentManager.Load<Texture2D>("Slasher");
		}

		class SwordSlash {
			public Vector2 Position;
			public float Width;
			public float Height;
			public double DelayTime;
			public double DelayCounter;

			public SwordSlash(Vector2 position) {
				Position = position;
				Width = 20;
				Height = 20;
				DelayTime = 200;
				DelayCounter = 0;
			}
		}
		/// <summary>
		/// How the slash can be drawed
		/// </summary>
		public void DrawSwordSlash() {
			for (int i = 0; i < listSwordSlashes.Count; i++) {
				var slash = listSwordSlashes[i];
				//find the center of the element
				var posX = (int)(slash.Position.X - 10);
				var posY = (int)(slash.Position.Y - 10);
				if (slash.Position != tempPos) {
					spriteBatch.Draw(swordSlash, new Rectangle(posX, posY, (int)slash.Width, (int)slash.Height), Color.Red);
				}
				else {
					//remove the element at tempPos
					listSwordSlashes.Remove(slash);
				}
			}
		}


		public void DrawSlashEffect1() { }

		public void DrawSlashEffect2() { }

		/// <summary>
		/// Draw all the effects
		/// </summary>
		public void Draw() {
			spriteBatch.Begin();

			DrawSwordSlash();

			spriteBatch.End();
		}
		
		/// <summary>
		/// Update Effects status
		/// </summary>
		/// <param name="gameTime">Provide a snapshot of time value</param>
		public void Update(GameTime gameTime) {
			#region DrawSwordSlash() logic
			lastPos = currentPos;
			touchCollection = TouchPanel.GetState();
			foreach (var touch in touchCollection) {
				currentPos = touch.Position;
				//calculate distance between last and current position
				var x1 = lastPos.X;
				var x2 = currentPos.X;
				var y1 = lastPos.Y;
				var y2 = currentPos.Y;
				var x = x1 - x2;
				var y = y1 - y2;
				distance = System.Math.Sqrt(x * x + y * y);
				additionElements = System.Convert.ToInt16(distance / 1);//1.5 is the smallest distance
				//find the equation ( y = a * x + b ) of the vector connect lastPos and currentPos
				var a = y / x;
				var b = y1 - a * x1;
				//each 1.5(unit) add a new element
				for (double i = 0, distanceFromLastPos = 1; i < additionElements; i++, distanceFromLastPos -= 1) {
					var ratio = distanceFromLastPos / distance;
					var elementX = (float)(lastPos.X + x * ratio);
					var elementY = a * elementX + b;//y = ax + b
					var element = new SwordSlash(new Vector2(elementX, elementY));
					listSwordSlashes.Add(element);
				}
				//add a new element at current position
				listSwordSlashes.Add(new SwordSlash(currentPos));
			}


			//for each swordSlash element, check it's DelayCounter, set it's position to tempPos if greater than Delay time. In Draw(), remove all element at tempPos
			//In this step, also calculate the width of the element
			for (int i = 0; i < listSwordSlashes.Count; i++) {
				SwordSlash slash = listSwordSlashes[i];
				if (slash.DelayCounter < slash.DelayTime) {
					//Count the DelayCounter value
					slash.DelayCounter += gameTime.ElapsedGameTime.TotalMilliseconds;
					//Calculate the width of element
					var ratio = slash.DelayCounter / slash.DelayTime;
					slash.Width = 16 * (float)(1.2 - ratio);
					slash.Height = 16 * (float)(1.2 - ratio);
				}
				else {
					slash.Position = tempPos;
				}
			}
			#endregion

			#region DrawSwordSlashEffect1() logic
			#endregion

			#region DrawSwordSlashEffect2() logic
			#endregion

		}
	}
}